package com.hezuti.common.url;

import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 支持访问互联网<br/>
 * @author <a href="mailto:douxiaoyu@yicha.cn">Dxy</a><br/>
 * @see
 * 定义了一些访问互联网基本属性<br/>
 * 添加了{@link HostManager} 管理互联网的地址<br/>
 * 继承本类的实现类都要重新定义 NAME_SPACE 的值<br/>
 * 本类的实现类通过spring配置文件实例化<br/><br/>
 * 使用样例<{@link ReleatedWord}>：<br/>
 * {@code <!-- 相关词 -->}<br/>
 * {@code<bean id="releatedWord" class="cn.yicha.common.data.releatedword.ReleatedWord">}<br/>
 * {@code		<property name="dataId" value="releatedWord" />}<br/>
 * {@code		<property name="host" value="123.125.213.134:18037:100" />}<br/>
 * {@code		<property name="uri" value="/RelatedWord/MeituRW" />}<br/>
 * {@code	</bean>}
 */
public abstract  class HostSupport {
    public static final String QUERY_ENCODE="queryEncode";
    public static final String ENCODE="encode";
    /**全局唯一命名空间，与资源id共同组成全局唯一id,继承者需要重新赋值*/
    public  String NAME_SPACE="_";
    /**本资源下唯一标识*/
    protected String dataId=UUID.randomUUID().toString();
    protected String queryEncode="utf-8";
    protected String encode="utf-8";	
    protected int readTimeOut=5000;
    protected int connectTimeOut=5000;
    
    /** 日志 */
    protected Log		 logger      = LogFactory.getLog(getClass());
    /** 互联网管理对象,存放着应用所有的ip */
    @Autowired
    protected HostManager	   hostManager;
    
    protected String getHost(){
	return hostManager.getHost(NAME_SPACE+dataId);
    }
    
    protected String getUri(){
	return hostManager.getUri(NAME_SPACE+dataId);
    }
    
    public String getDataId() {
        return dataId;
    }
    public String getQueryEncode() {
        return queryEncode;
    }
    public void setQueryEncode(String queryEncode) {
        this.queryEncode = queryEncode;
    }
    public String getEncode() {
        return encode;
    }
    public void setEncode(String encode) {
        this.encode = encode;
    }
    public int getReadTimeOut() {
        return readTimeOut;
    }
    public void setReadTimeOut(int readTimeOut) {
        this.readTimeOut = readTimeOut;
    }
    public int getConnectTimeOut() {
        return connectTimeOut;
    }
    public void setConnectTimeOut(int connectTimeOut) {
        this.connectTimeOut = connectTimeOut;
    }
    
    /**
     * 将配置文件中的host配置存储到全局hostManager中
     * 
     * @param host
     */
    public void setHost(String host){
	/**用全局唯一表示存储*/
	hostManager.addHost(NAME_SPACE+dataId, host);
    }
    /**
     * 将配置文件中的uri配置存储到全局hostManager中;
     * @param uri
     */
    public void setUri(String uri){
	
	hostManager.addUri(NAME_SPACE+dataId, uri);
    }
    /**
     * 资源 id 配置,如果不配置 默认使用uuid作为唯一资源标识
     * 
     * @param dataId
     */
    public void setDataId(String dataId){
	this.dataId=dataId;
    }


    
}
